import itertools

from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session
from fastapi.responses import Response

from common.database import get_db
from core.response import resp_400, resp_200
from app.callback.handlers.prom_alarm_callback_handler import PromAlarmCallbackHandler
from app.callback.handlers.callback_host_disk_handler import CallbackHostDiskHandler
from app.callback.handlers.callback_container_cpu_handler import CallbackContainerCpuHandler
from app.callback.handlers.callback_pod_heal_handler import CallbackPodHealHandler
from app.callback.handlers.callback_rabbitmq_queue_handler import CallbackRabbitmqQueueHandler
from app.callback.handlers.prometheus_metrics_handler import PrometheusMetricsHandler
from app.callback.schemas import *
from core.logger import logger
from common.welab_k8s import init_k8s


router = APIRouter()


#### 告警自愈接口 ####
@router.post('/pod/heal/', summary="pod重启次数后还没恢复", description="监控-回调")
async def post_heal_pod_abnormal(data: dict, db: Session = Depends(get_db), k8s:None = Depends(init_k8s)):
    callback_pod_heal_handler = CallbackPodHealHandler()
    result = callback_pod_heal_handler.post_heal_pod_abnormal(db, data)
    if not result:
        message = "post heal pod abnormal failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()



@router.post('/container/cpuusage/', summary="生产业务服务cpu平均使用率过高", description="监控-回调")
async def post_container_cpu_usage(data: dict, db: Session = Depends(get_db)):
    callback_container_cpu_handler = CallbackContainerCpuHandler()
    result = callback_container_cpu_handler.post_container_cpu_usage(db, data)
    if not result:
        message = "post container cpu usage failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()


@router.post('/host/diskusage/', summary="服务器磁盘使用率过高", description="监控-回调")
async def post_host_disk_usage(data: dict, db: Session = Depends(get_db)):
    callback_host_disk_handler = CallbackHostDiskHandler()
    result = callback_host_disk_handler.post_host_disk_usage(db, data)
    if not result:
        message = "post post host disk usage failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()


@router.post('/rabbitmq/queue/', summary="rabbitmq队列消息祖塞", description="监控-回调")
async def callback_rabbitmq_queue(data: dict, db: Session = Depends(get_db)):
    callback_rabbitmq_queue_handler = CallbackRabbitmqQueueHandler()
    result = callback_rabbitmq_queue_handler.post_rabbitmq_queue(db, data)
    if not result:
        message = "post rabbitmq queue failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()




#### alertmanager告警回调接口 ####
@router.post('/alertmanager/', summary="alertmanager告警回调接口", description="监控-回调")
async def post_alarm_callback(data: PostAlarmCallback, db: Session = Depends(get_db)):
    prom_alarm_callback_handler = PromAlarmCallbackHandler()
    result = prom_alarm_callback_handler.handle_alerts(db, data.dict())
    if not result:
        message = "post alarm callback failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.get('/status/', summary="告警系统的状态", description="监控-回调")
async def get_alarm_status(db: Session = Depends(get_db)):
    prom_alarm_callback_handler = PromAlarmCallbackHandler()
    result = prom_alarm_callback_handler.get_alarm_status(db)
    if not result:
        message = "get alarm status failed"
        logger.error(message)
        return resp_400(message)
    return resp_200()



@router.get('/metrics/', summary="获取prometheus的metrics格式信息", description="监控-回调")
async def get_all_metrics(db: Session = Depends(get_db)):
    prometheus_metrics_handler = PrometheusMetricsHandler()
    result = prometheus_metrics_handler.get_all_metrics(db)
    if not result:
        message = "get prometheus metrics failed"
        logger.error(message)
        return resp_400(message)
    return Response(content=result, media_type="text/plain") 